﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PhiStore.ExampleCodes.PhiElection;
using PhiStore.ExampleCodes.PhiLog;
using PhiStore.ExampleCodes.PhiMovieStreamer;
using PhiStore.Generics;
using PhiStore.Policies;
using PhiStore.Tests;

namespace PhiStore
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Experiments_Click(object sender, EventArgs e)
        {
            using (Experiments.ExperimentsUI ui = new Experiments.ExperimentsUI())
            {
                ui.ShowDialog();
            }
        }

        private void Testing_Click(object sender, EventArgs e)
        {
            using (TestUI test = new TestUI())
            {
                test.ShowDialog();
            }
        }

        private void SimpleExampleButton_Click(object sender, EventArgs e)
        {
            try
            {
                PhiClient<int> client = new PhiClient<int>();
                client.Initialize();
                client.Connect();

                string key = "example_list";
                List<int> values = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 });
                Policy policy = new Policy();

                client.ListCreate(key, values, policy);
                client.ListDelete(key, 2, 1); //deletes "3"
                client.ListUpdate(key, 0, 0); //"1" is now "0"
                client.ListAppend(key, 7); //adds 7 to end of list

                client.Delete(key); //completely remove

                client.Disconnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void philog_Click(object sender, EventArgs e)
        {
            using (PhiLogUI ui = new PhiLogUI())
            {
                ui.ShowDialog();
            }
        }

        private void ElectionGenerator_Click(object sender, EventArgs e)
        {
            using (ElectionGenerator eg = new ElectionGenerator())
            {
                eg.ShowDialog();
            }
        }

        private void ElectionReader_Click(object sender, EventArgs e)
        {
            using (ElectionReader er = new ElectionReader())
            {
                er.ShowDialog();
            }
        }

        private void MoviePlayer_Click(object sender, EventArgs e)
        {
            using (MoviePlayer mp = new MoviePlayer())
            {
                mp.ShowDialog();
            }
        }

        private void PhiArray_Test_Click(object sender, EventArgs e)
        {
            PhiClient<object> client = new PhiClient<object>(false, true);
            client.Initialize();
            client.Connect();
            PhiArray<double> array;
            try
            {
                array = new PhiArray<double>(client, "dummy");
            }
            catch (ReturnCodes.PhiException)
            {
                array = new PhiArray<double>(client, "dummy", 50);
            }

            for (long i = 0; i < array.Count; i++)
            {
                array[i] = Convert.ToDouble(i);
            }

            bool success = true;
            for (long i = 0; i < array.Count; i++)
            {
                success &= Convert.ToDouble(i) == array[i];
                Console.WriteLine(array[i].ToString("N1"));
            }

            foreach (double d in array)
            {
                Console.WriteLine(d.ToString("N1"));
            }


            if (success)
                MessageBox.Show("hurray");
            else
                MessageBox.Show("crap");

            client.Delete("dummy");

            client.Disconnect();
        }
    }
}
